<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="生产线类别" prop="lineType1" label-width="120">
        <el-select v-model="queryParams.lineType1" placeholder="请选择生产线类别" clearable>
          <el-option
            v-for="dict in dict.type.line_type1"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="生产线编号" prop="lineId" label-width="120">
        <el-input
          v-model="queryParams.lineId"
          placeholder="请输入生产线编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生产线名称" prop="lineName" label-width="120">
        <el-input
          v-model="queryParams.lineName"
          placeholder="请输入生产线名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['base:productionLine:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['base:productionLine:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['base:productionLine:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['base:productionLine:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
        >导入</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table height="calc(100vh - 300px)" v-loading="loading" :data="productionLineList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="公司名称" align="center" prop="factoryName" />
      <el-table-column label="公司代码" align="center" prop="factoryCode" />
      <el-table-column label="生产线类别" align="center" prop="lineType1">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.line_type1" :value="scope.row.lineType1"/>
        </template>
      </el-table-column>
      <el-table-column label="生产线类型" align="center" prop="lineType2" />
      <el-table-column label="生产线编号" align="center" prop="lineId" />
      <el-table-column label="生产线名称" align="center" prop="lineName" />
      <el-table-column label="生产线简码" align="center" prop="lineSimpleCode" />
      <el-table-column label="部门名称" align="center" prop="deptName" />
      <el-table-column label="部门代码" align="center" prop="deptId" />
      <el-table-column label="物流公司代码" align="center" prop="logisticsId" />
      <el-table-column label="物流公司名称" align="center" prop="logisticsName" />
      <el-table-column label="生产节拍" align="center" prop="pBeat" />
      <el-table-column label="单班产能" align="center" prop="dbcn" />
      <el-table-column label="配送时间" align="center" prop="psTime" />
      <el-table-column label="拆箱时间" align="center" prop="csTime" />
      <el-table-column label="布线数量" align="center" prop="bxNum" />
      <el-table-column label="配送节拍" align="center" prop="psBeat" />
      <el-table-column label="分拣时间" align="center" prop="fjTime" />
      <el-table-column label="分装时间" align="center" prop="fzTime" />
      <el-table-column label="提前时间" align="center" prop="tqTime" />
      <el-table-column label="状态" align="center" prop="lineState" />
      <el-table-column label="工位数量" align="center" prop="gwNum" />
      <el-table-column label="投料位数量" align="center" prop="tlwNum" />
      <el-table-column label="提前时段" align="center" prop="tqSd" />
      <el-table-column label="后续生产线" align="center" prop="hxLineId" />
      <el-table-column label="分装线数量" align="center" prop="fzxNum" />
      <el-table-column label="默认工位" align="center" prop="mrGw" />
      <el-table-column label="排序" align="center" prop="seq" />
      <el-table-column label="相邻工位" align="center" prop="xlGw" />
      <el-table-column label="移位时间(秒)" align="center" prop="ywTime" />
      <el-table-column label="工位顺序" align="center" prop="gwSeq" />
      <el-table-column label="工位说明" align="center" prop="gwRemark" />
      <el-table-column label="关联目的地" align="center" prop="glMdd" />
      <el-table-column label="关联来源" align="center" prop="glLy" />
      <el-table-column label="生产缓存区" align="center" prop="scHcq" />
      <el-table-column label="线边库代码" align="center" prop="xbStockId" />
      <el-table-column label="线边库名称" align="center" prop="xbStockName" />
      <el-table-column label="上线仓库编码" align="center" prop="sxStockId" />
      <el-table-column label="上线仓库名称" align="center" prop="sxStockName" />
      <el-table-column label="上线单元控制" align="center" prop="sxDykz" />
      <el-table-column label="拉动方式" align="center" prop="ldFs" />
      <el-table-column label="拉动控制时间" align="center" prop="ldTime" />
      <el-table-column label="拉动地址" align="center" prop="ldAddress" />
      <el-table-column label="PC仓库代码" align="center" prop="pcStockId" />
      <el-table-column label="PC仓库名称" align="center" prop="pcStockName" />
      <el-table-column label="退料仓库代码" align="center" prop="tlStockId" />
      <el-table-column label="退料仓库名称" align="center" prop="tlStockName" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column width="120" fixed="right" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['base:productionLine:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['base:productionLine:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改生产线信息维护对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="公司名称" prop="factoryName">
          <el-input v-model="form.factoryName" placeholder="请输入公司名称" />
        </el-form-item>
        <el-form-item label="公司代码" prop="factoryCode">
          <el-input v-model="form.factoryCode" placeholder="请输入公司代码" />
        </el-form-item>
        <el-form-item label="生产线类别" prop="lineType1">
          <el-select v-model="form.lineType1" placeholder="请选择生产线类别">
            <el-option
              v-for="dict in dict.type.line_type1"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="生产线编号" prop="lineId">
          <el-input v-model="form.lineId" placeholder="请输入生产线编号" />
        </el-form-item>
        <el-form-item label="生产线名称" prop="lineName">
          <el-input v-model="form.lineName" placeholder="请输入生产线名称" />
        </el-form-item>
        <el-form-item label="生产线简码" prop="lineSimpleCode">
          <el-select v-model="form.lineSimpleCode" placeholder="请选择生产线类别">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="部门名称" prop="deptName">
          <el-input v-model="form.deptName" placeholder="请输入部门名称" />
        </el-form-item>
        <el-form-item label="部门代码" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入部门代码" />
        </el-form-item>
        <el-form-item label="物流公司代码" prop="logisticsId">
          <el-input v-model="form.logisticsId" placeholder="请输入物流公司代码" />
        </el-form-item>
        <el-form-item label="物流公司名称" prop="logisticsName">
          <el-input v-model="form.logisticsName" placeholder="请输入物流公司名称" />
        </el-form-item>
        <el-form-item label="生产节拍" prop="pBeat">
          <el-input v-model="form.pBeat" placeholder="请输入生产节拍" />
        </el-form-item>
        <el-form-item label="单班产能" prop="dbcn">
          <el-input v-model="form.dbcn" placeholder="请输入单班产能" />
        </el-form-item>
        <el-form-item label="配送时间" prop="psTime">
          <el-input v-model="form.psTime" placeholder="请输入配送时间" />
        </el-form-item>
        <el-form-item label="拆箱时间" prop="csTime">
          <el-input v-model="form.csTime" placeholder="请输入拆箱时间" />
        </el-form-item>
        <el-form-item label="布线数量" prop="bxNum">
          <el-input v-model="form.bxNum" placeholder="请输入布线数量" />
        </el-form-item>
        <el-form-item label="配送节拍" prop="psBeat">
          <el-input v-model="form.psBeat" placeholder="请输入配送节拍" />
        </el-form-item>
        <el-form-item label="分拣时间" prop="fjTime">
          <el-input v-model="form.fjTime" placeholder="请输入分拣时间" />
        </el-form-item>
        <el-form-item label="分装时间" prop="fzTime">
          <el-input v-model="form.fzTime" placeholder="请输入分装时间" />
        </el-form-item>
        <el-form-item label="提前时间" prop="tqTime">
          <el-input v-model="form.tqTime" placeholder="请输入提前时间" />
        </el-form-item>
        <el-form-item label="状态" prop="lineState">
          <el-input v-model="form.lineState" placeholder="请输入状态" />
        </el-form-item>
        <el-form-item label="工位数量" prop="gwNum">
          <el-input v-model="form.gwNum" placeholder="请输入工位数量" />
        </el-form-item>
        <el-form-item label="投料位数量" prop="tlwNum">
          <el-input v-model="form.tlwNum" placeholder="请输入投料位数量" />
        </el-form-item>
        <el-form-item label="提前时段" prop="tqSd">
          <el-input v-model="form.tqSd" placeholder="请输入提前时段" />
        </el-form-item>
        <el-form-item label="后续生产线" prop="hxLineId">
          <el-input v-model="form.hxLineId" placeholder="请输入后续生产线" />
        </el-form-item>
        <el-form-item label="分装线数量" prop="fzxNum">
          <el-input v-model="form.fzxNum" placeholder="请输入分装线数量" />
        </el-form-item>
        <el-form-item label="默认工位" prop="mrGw">
          <el-input v-model="form.mrGw" placeholder="请输入默认工位" />
        </el-form-item>
        <el-form-item label="排序" prop="seq">
          <el-input v-model="form.seq" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="相邻工位" prop="xlGw">
          <el-input v-model="form.xlGw" placeholder="请输入相邻工位" />
        </el-form-item>
        <el-form-item label="移位时间(秒)" prop="ywTime">
          <el-input v-model="form.ywTime" placeholder="请输入移位时间(秒)" />
        </el-form-item>
        <el-form-item label="工位顺序" prop="gwSeq">
          <el-input v-model="form.gwSeq" placeholder="请输入工位顺序" />
        </el-form-item>
        <el-form-item label="工位说明" prop="gwRemark">
          <el-input v-model="form.gwRemark" placeholder="请输入工位说明" />
        </el-form-item>
        <el-form-item label="关联目的地" prop="glMdd">
          <el-input v-model="form.glMdd" placeholder="请输入关联目的地" />
        </el-form-item>
        <el-form-item label="关联来源" prop="glLy">
          <el-input v-model="form.glLy" placeholder="请输入关联来源" />
        </el-form-item>
        <el-form-item label="生产缓存区" prop="scHcq">
          <el-input v-model="form.scHcq" placeholder="请输入生产缓存区" />
        </el-form-item>
        <el-form-item label="线边库代码" prop="xbStockId">
          <el-input v-model="form.xbStockId" placeholder="请输入线边库代码" />
        </el-form-item>
        <el-form-item label="线边库名称" prop="xbStockName">
          <el-input v-model="form.xbStockName" placeholder="请输入线边库名称" />
        </el-form-item>
        <el-form-item label="上线仓库编码" prop="sxStockId">
          <el-input v-model="form.sxStockId" placeholder="请输入上线仓库编码" />
        </el-form-item>
        <el-form-item label="上线仓库名称" prop="sxStockName">
          <el-input v-model="form.sxStockName" placeholder="请输入上线仓库名称" />
        </el-form-item>
        <el-form-item label="上线单元控制" prop="sxDykz">
          <el-input v-model="form.sxDykz" placeholder="请输入上线单元控制" />
        </el-form-item>
        <el-form-item label="拉动方式" prop="ldFs">
          <el-input v-model="form.ldFs" placeholder="请输入拉动方式" />
        </el-form-item>
        <el-form-item label="拉动控制时间" prop="ldTime">
          <el-input v-model="form.ldTime" placeholder="请输入拉动控制时间" />
        </el-form-item>
        <el-form-item label="拉动地址" prop="ldAddress">
          <el-input v-model="form.ldAddress" placeholder="请输入拉动地址" />
        </el-form-item>
        <el-form-item label="PC仓库代码" prop="pcStockId">
          <el-input v-model="form.pcStockId" placeholder="请输入PC仓库代码" />
        </el-form-item>
        <el-form-item label="PC仓库名称" prop="pcStockName">
          <el-input v-model="form.pcStockName" placeholder="请输入PC仓库名称" />
        </el-form-item>
        <el-form-item label="退料仓库代码" prop="tlStockId">
          <el-input v-model="form.tlStockId" placeholder="请输入退料仓库代码" />
        </el-form-item>
        <el-form-item label="退料仓库名称" prop="tlStockName">
          <el-input v-model="form.tlStockName" placeholder="请输入退料仓库名称" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :close-on-click-modal="false" :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <!--  <div class="el-upload__tip" slot="tip">
              <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的用户数据
            </div>-->
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProductionLine, getProductionLine, delProductionLine, addProductionLine, updateProductionLine } from "@/api/base/productionLine";
import {getToken} from "@/utils/auth";

export default {
  name: "ProductionLine",
  dicts: ['line_type1'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 生产线信息维护表格数据
      productionLineList: [],
      // 弹出层标题
      title: "",
      options: [{
        value: '0',
        label: '下料线'
      }, {
        value: '1',
        label: '1吨线'
      }, {
        value: '2',
        label: '750线'
      }, {
        value: '3',
        label: '560线'
      }, {
        value: '4',
        label: '250线'
      }],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        lineType1: null,
        lineType2: null,
        lineId: null,
        lineName: null,
      },
      // 表单参数
      form: {},
      //生产线数据导入
      upload: {
        // 是否显示弹出层（物料导入）
        open: false,
        // 弹出层标题（物料导入）
        title: "导入生产线信息",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/base/productionLine/importData"
      },
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询生产线信息维护列表 */
    getList() {
      this.loading = true;
      listProductionLine(this.queryParams).then(response => {
        this.productionLineList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        factoryName: null,
        factoryCode: null,
        lineType1: null,
        lineType2: null,
        lineId: null,
        lineName: null,
        deptName: null,
        deptId: null,
        logisticsId: null,
        logisticsName: null,
        pBeat: null,
        dbcn: null,
        psTime: null,
        csTime: null,
        bxNum: null,
        psBeat: null,
        fjTime: null,
        fzTime: null,
        tqTime: null,
        lineState: null,
        gwNum: null,
        tlwNum: null,
        tqSd: null,
        hxLineId: null,
        fzxNum: null,
        mrGw: null,
        seq: null,
        xlGw: null,
        ywTime: null,
        gwSeq: null,
        gwRemark: null,
        glMdd: null,
        glLy: null,
        scHcq: null,
        xbStockId: null,
        xbStockName: null,
        sxStockId: null,
        sxStockName: null,
        sxDykz: null,
        ldFs: null,
        ldTime: null,
        ldAddress: null,
        pcStockId: null,
        pcStockName: null,
        tlStockId: null,
        tlStockName: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加生产线信息维护";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getProductionLine(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改生产线信息维护";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateProductionLine(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProductionLine(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除生产线信息维护编号为"' + ids + '"的数据项？').then(function() {
        return delProductionLine(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('base/productionLine/export', {
        ...this.queryParams
      }, `productionLine_${new Date().getTime()}.xlsx`)
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "生产线导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('base/productionLine/importTemplate', {
      }, `生产线数据信息.xlsx`)
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
  }
};
</script>
